<template>
  <div class="pf-helper pf-bgwhite pf-p-10">
    <el-empty description="功能建设中..."></el-empty>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, Ref, onMounted } from 'vue';
import { systemApi } from '@/constants/api/system-api';
import { useNotice } from '@/components/element-plus/notice';
import {
  useData,
  useDict,
  emptyForm,
  FormModel,
  useHttpClient,
  emptyTable,
  TableModel,
  SysDict,
  ResponseCodeEnum
} from 'pf-component';
import { FormNameEnum } from '@/constants/enum/form-name.enum';
import { TableNameEnum } from '@/constants/enum/table-name.enum';

export default defineComponent({
  name: 'Helper',
  setup() {
    const { message } = useNotice();
    const dictConfig: Ref<TableModel> = ref(emptyTable);
    const dictData: Ref<SysDict[]> = ref([]);
    const show: Ref<boolean> = ref(false);

    const formConfig: Ref<FormModel> = ref(emptyForm);
    const formInfo: Ref<SysDict> = ref(null as any);
    const dictForm = ref(null);
    onMounted(() => {
      // const formPromise = useData().loadForm([
      //   {
      //     name: FormNameEnum.sysDictForm,
      //     config: formConfig,
      //     info: formInfo
      //   }
      // ]);
      // const tablePromise = useData().loadTable([{ name: TableNameEnum.sysDict, config: dictConfig }]);
      // Promise.all([formPromise, tablePromise]).then((res: boolean[]) => {
      //   select();
      // });
    });

    const select = () => {
      useHttpClient()
        .general<SysDict[]>(systemApi.dictApi.list)
        .then(res => {
          if (res.code === ResponseCodeEnum.SUCCESS) {
            dictData.value = res.data;
          } else {
            message.error(res.message);
          }
        });
    };
    const create = () => {
      formInfo.value = useData().toFormValue(formConfig.value as any);
      show.value = true;
    };
    const confirmCreate = () => {
      (dictForm.value as any).validate((val: boolean) => {
        if (val) {
          useHttpClient()
            .general(systemApi.dictApi.update, undefined, formInfo.value)
            .then(res => {
              if (res.code === ResponseCodeEnum.SUCCESS) {
                show.value = false;
                select();
              } else {
                message.error(res.message);
              }
            });
        }
      });
    };
    const del = () => {
      // systemApi.dictApi.delete
      message.error('待完善');
    };
    const handleClick = (data: SysDict, cmd: string) => {
      switch (cmd) {
        case 'edit':
          formInfo.value = data;
          show.value = true;
          break;
        default:
          message.error('未定义的操作！');
      }
    };
    return {
      select,
      dictData,
      dictConfig,
      handleClick,
      create,
      confirmCreate,
      del,
      show,
      dictForm,
      formConfig,
      formInfo
    };
  }
});
</script>

<style scoped lang="scss">
.#{$prefix} {
  &-helper {
  }
}
</style>
